/*
 * Part of knowledge engineering (ke) course work, 2010/11
 */
package se.bth.ke.firstfriend.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author markus.goetz
 * @author nico.rehwaldt
 */
@Entity
@NamedQueries({
    @NamedQuery(
        name="Todo.getTodoInfosByUser",
        query="SELECT new se.bth.ke.firstfriend.model.dto.TodoInfo(t, a, t.currentCheckPoint) " +
              "  FROM Todo t JOIN t.article a " +
              "  WHERE t.user = :user"),
    @NamedQuery(
        name="Todo.getTodoInfoByIdAndUser",
        query="SELECT new se.bth.ke.firstfriend.model.dto.TodoInfo(t, a, t.currentCheckPoint) " +
              "  FROM Todo t JOIN t.article a " +
              "  WHERE t.user = :user AND t.id = :id")
})
@XmlRootElement
public class Todo extends BookmarkableItem {
    
    private int priority;
    
    private String comment;
    
    @Temporal(TemporalType.TIMESTAMP)
    private Date deadline;
    
    @OneToMany(mappedBy="todo", cascade=CascadeType.ALL)
    private List<CheckPoint> checkPoints;
    
    @OneToOne
    private CheckPoint currentCheckPoint;
                                           
    public Todo() {}

    public Todo(Article article, int priority, Date deadline) {
        super(article);
        this.priority = priority;
        this.deadline = deadline;
    }

    public Date getDeadline() {
        return deadline;
    }

    /**
     * @param deadline the deadline to set
     */
    public void setDeadline(Date deadline) {
        this.deadline = deadline;
    }
    
    public int getPriority() {
        return priority;
    }

    public String getComment() {
        return this.comment;
    }

    public Todo setPriority(int priority) {
        this.priority = priority;
        return this;
    }

    public Todo setComment(String comment) {
        this.comment = comment;
        return this;
    }

    /**
     * @return list of checkpoints stored for the todo
     */
    public List<CheckPoint> getCheckPoints() {
        if (checkPoints == null) {
            checkPoints = new ArrayList();
        }
        return checkPoints;
    }

    /**
     * Adds a check point to this todo
     * @param checkPoint
     */
    public void addCheckPoint(CheckPoint checkPoint) {
        getCheckPoints().add(checkPoint);
        checkPoint.setTodo(this);

        this.currentCheckPoint = checkPoint;
    }

    public Todo done() {
        addCheckPoint(new CompletedCheckPoint());
        return this;
    }

    public Todo remove() {
        addCheckPoint(CheckPoint.deleted());
        return this;
    }
    
    @Override
    public String toString() {
        return "se.bth.ke.firstfriend.model.Todo[id=" + getId() + "]";
    }
}
